package gov.mogaha.ntis.cmm.mob.baseactivity;

import gov.mogaha.ntis.cmm.mob.R;
import gov.mogaha.ntis.cmm.mob.servlet.CmmMobServlet;
import gov.mogaha.ntis.cmm.mob.servlet.CmmMobServletHandler;
import gov.mogaha.ntis.cmm.mob.util.DisPlayMetricsManager;
import gov.mogaha.ntis.cmm.mob.util.Toaster;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public  class BaseActivity extends Activity {

	protected final static int DIALOG_ID_GPS_SETTING = 0;
	protected final static int DIALOG_ID_HTTP_ERROR = 1;
	protected final static int DIALOG_ID_HTTP_CERT = 2;
	public final static int DIALOG_ID_HTTP = 3;
	protected final static int DIALOG_ID_CLOSE = 4;
	
	protected final static int INTENT_REGUEST_CODE_GPS = 0;
	protected final static int INTENT_REGUEST_CODE_DEFAULT = 19830605;

	public static int progressMsgId = R.string.http_loading;
	public static CmmMobServlet.DataSet servletData;
	public static Activity mActivity;
	
	protected static int parent_pid = -1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		DisPlayMetricsManager.setDensity(this);
		mActivity = this;
		setParentProecssId();
	}
	
	protected Handler mDialogHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case DIALOG_ID_GPS_SETTING:
				showDialog(DIALOG_ID_GPS_SETTING);
				break;
			case DIALOG_ID_CLOSE:
				showDialog(DIALOG_ID_CLOSE);
				break;
			default:
				break;
			}
		};
	};
	
	Handler mToastHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			Toaster.showMsg(BaseActivity.this, msg.what);
		};
	};
	 
	
	
	protected void showMsg(int resId){
		mToastHandler.sendEmptyMessage(resId);
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch(id){
		case DIALOG_ID_GPS_SETTING:
			return new AlertDialog.Builder(BaseActivity.this)
			.setTitle(getString(R.string.messagebox_title_notice))
			.setMessage(getResources().getString(R.string.gpssetting))
			.setPositiveButton(getResources().getString(R.string.messagebox_ok), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
					intent.addCategory(Intent.CATEGORY_DEFAULT);
					startActivityForResult(intent, INTENT_REGUEST_CODE_GPS);
				}
			})
			.setNegativeButton(getResources().getString(R.string.messagebox_cancel), null)
			.show();
		case DIALOG_ID_CLOSE:	//앱종료
			return new AlertDialog.Builder(BaseActivity.this)
			.setIcon(R.drawable.alert_dialog_icon)
			.setTitle(getString(R.string.app_name))
			.setMessage(getResources().getString(R.string.mainclosemsg))
			.setPositiveButton(getResources().getString(R.string.messagebox_ok), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					//application 프로세스를 강제 종료한다
					moveTaskToBack(true);
					finish();
					
					android.os.Process.killProcess(parent_pid);	// 통합App 종료
					android.os.Process.killProcess(android.os.Process.myPid());	// 업무 App 종료
				}
			})
			.setNegativeButton(getResources().getString(R.string.messagebox_cancel), null)
			.show();
		case CmmMobServletHandler.PROGRESS_DIALOG:
			return ProgressDialog.show(BaseActivity.this, "", getString(progressMsgId),false,true);
		default:
			break;
		}
		return null;		
	};
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if(resultCode == RESULT_OK && data != null){
			switch(requestCode){
			case INTENT_REGUEST_CODE_GPS:
				break;
			default:
				break;
			}
		} else if(resultCode == RESULT_FIRST_USER && data != null){
			setResult(RESULT_FIRST_USER, data);
			finish();
		}
	}
	
	protected void setParentProecssId() {
		Intent mIntent = getIntent();
		if(parent_pid == -1){
			try{
				Log.d("debug", "Base sid:"+mIntent.getStringExtra("sid"));
				Log.d("debug", "pid:"+mIntent.getIntExtra("pid", -1));
				parent_pid = mIntent.getIntExtra("pid", -1);
			}catch (Exception e) {
				finish();
			}
		}
		
	}
}
